package edu.bit.water.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;

import edu.bit.water.controller.MetaEvaWbalTestController;
import edu.bit.water.dao.MetaEvaWbalTestDao;
import edu.bit.water.entity.Column;
import edu.bit.water.entity.MetaEvaWbalTest;
import edu.bit.water.entity.MetaEvaWuseInfo;

@Service
@Component
public class MetaEvaWbalTestService {

	// 记得加上这个哇，这是告诉全世界，你要开始在这类里面使用日志
	protected static final Logger logger = LoggerFactory.getLogger(MetaEvaWbalTestController.class);

	@Autowired
	public MetaEvaWbalTestDao metaEvaWbalTestDao;
	@Autowired
	public MetaEvaWbalTestAssociationService metaEvaWbalTestAssociationService;
	@Autowired
	public MetaEvaWbalTestService metaEvaWbalTestService;
	private boolean flag = false;// 用来记录实体表字段是否发生改变，true为已改变，false为改变
	private List<Column> list = null;
	private List<MetaEvaWbalTest> listAll = null;
	/**
     * @return 返回所有字段
     */
    public List<Column> selectColumn() {
        if (list == null || flag) {
            list = metaEvaWbalTestDao.selectColumn();
            flag = false;
        }
        return list;
    }
	
    /**
     * @return 返回所有字段
     */
    public List<MetaEvaWbalTest> selectColumnAll() {
        if (listAll == null || flag) {
            listAll = metaEvaWbalTestDao.queryMetaEvaWbalTest();
            flag = false;
        }
        return listAll;
    }
    
    /**
     * @return 返回list
     * @throws JsonProcessingException 
     */
    public ArrayList<MetaEvaWbalTest> queryMetaEvaWbalTest() throws JsonProcessingException {
        ArrayList<MetaEvaWbalTest> list = metaEvaWbalTestDao.queryMetaEvaWbalTest();
        return list;
    }
    
    /**
     * 查詢同組列數
     * @param faLevel 組號
     * @return 同組列數
     */
    public int queryColumnNumByFaLevel(int faLevel) {
    	return metaEvaWbalTestDao.queryColumnNumByFaLevel(faLevel);  
    }
    
    /**
     * @param metaSensor 元数据设备实体类
     * @return 成功返回1，失败返回0
     */
    public Map<String, Object> insertMetaEvaWbalTest(MetaEvaWbalTest metaEvaWbalTest) {
        Map<String, Object> modelMap = new HashMap<>();
        List<Column> list = metaEvaWbalTestDao.selectColumn();
        try {
	        metaEvaWbalTestDao.insertMetaEvaWbalTest(metaEvaWbalTest);
	        metaEvaWbalTestAssociationService.insertMetaEvaWbalTestAssociation(metaEvaWbalTest);
	        modelMap.put("status", "success");
	        flag = true;
        }catch (DuplicateKeyException e){
        	if(e.getMessage().contains("CS_NAME")) {
        		modelMap.put("status", "failed");
        		modelMap.put("msg", "中文名称已存在，请重新输入");
        	}
        	else if(e.getMessage().contains("EN_NAME")) {
        		modelMap.put("status", "failed");
        		modelMap.put("msg", "英文名称已存在，请重新输入");
        	}
        }
        return modelMap;
    }
    
    /**
     * @param enName 字段英文名称
     * @return 成功返回1，失败返回0
     */
    public int deleteMetaEvaWbalTest(List<String> enNameList) {
    	try {
    		for (String enName : enNameList) {
    			metaEvaWbalTestDao.deleteMetaEvaWbalTest(enName);
    		}
    		return 1;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		} finally {
			flag = true;
		}
	}

	/**
	 * @param enName 字段英文名称
	 * @return 成功返回1，失败返回0
	 */
	public int deleteMetaEvaWbalTest(String enName) {
		int n = metaEvaWbalTestDao.deleteMetaEvaWbalTest(enName);
		flag = true;
		return n;
	}


	public Map<String, Object> updateMetaEvaWbalTest(MetaEvaWbalTest metaEvaWbalTest) {



		Map<String, Object> modelMap = new HashMap<>();


		try{
			metaEvaWbalTestDao.updateMetaEvaWbalTest(metaEvaWbalTest);
			if(!metaEvaWbalTest.getOldName().equals(metaEvaWbalTest.getEnName())){
				metaEvaWbalTestAssociationService.updateMetaEvaWbalTestAssociation(metaEvaWbalTest);
			}

			modelMap.put("status", "success");


		}catch (DuplicateKeyException e){
			if(e.getMessage().contains("CS_NAME")) {
				modelMap.put("status", "failed");
				modelMap.put("msg", "中文名称已存在，请重新输入");
			}else if(e.getMessage().contains("EN_NAME")) {
				modelMap.put("status", "failed");
				modelMap.put("msg", "英文名称已存在，请重新输入");
			}
		}



		return modelMap;
	}
}
